﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CNVP.Framework.Helper;
using CNVP.Config;

namespace CNVP.JShop.DAL
{
    public class CNVP_DeliveryTemplate
    {
        private string Prefix = DBConfig.Prefix;
        #region DAL
        public int Insert(Model.CNVP_DeliveryTemplate obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into  " + Prefix + "DeliveryTemplate (DeliveryTemplateClassID, RectangleName, ItemName, MarginTop, MarginLeft, RectangleWidth, RectangleHight)");
            sql.Append("values(@DeliveryTemplateClassID, @RectangleName, @ItemName, @MarginTop, @MarginLeft, @RectangleWidth, @RectangleHight)");

            SqlParameter[] pra = {
                            new SqlParameter("@DeliveryTemplateClassID",obj.DeliveryTemplateClassID),
							new SqlParameter("@RectangleName",obj.RectangleName),
                            new SqlParameter("@ItemName",obj.ItemName),
							new SqlParameter("@MarginTop",obj.MarginTop),
                            new SqlParameter("@MarginLeft",obj.MarginLeft),
							new SqlParameter("@RectangleWidth",obj.RectangleWidth),
                            new SqlParameter("@RectangleHight",obj.RectangleHight)
                                 };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        public int Delete(Model.CNVP_DeliveryTemplate obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("delete from " + Prefix + "DeliveryTemplate where [DeliveryTemplateClassID] = @DeliveryTemplateClassID");
            SqlParameter[] pra = {
                                new SqlParameter("@DeliveryTemplateClassID",obj.DeliveryTemplateClassID)
                                };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        public List<Model.CNVP_DeliveryTemplate> LoadAll(string sqlWhere)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select ID, DeliveryTemplateClassID, RectangleName, ItemName, MarginTop, MarginLeft, RectangleWidth, RectangleHight from " + Prefix + "DeliveryTemplate");
            if (sqlWhere.Trim() != "")
            {
                sql.Append(" where " + sqlWhere);
            }
            DataTable DT = DbHelper.ExecuteTable(sql.ToString());
            List<Model.CNVP_DeliveryTemplate> list = new List<Model.CNVP_DeliveryTemplate>();
            if (DT != null && DT.Rows.Count > 0)
            {
                foreach (DataRow dr in DT.Rows)
                {
                    Model.CNVP_DeliveryTemplate cnvp_deliverytemplate = new Model.CNVP_DeliveryTemplate();
                    cnvp_deliverytemplate.ID = dr["ID"] is DBNull ? 0 : (int)dr["ID"];
                    cnvp_deliverytemplate.DeliveryTemplateClassID = dr["DeliveryTemplateClassID"] is DBNull ? 0 : (int)dr["DeliveryTemplateClassID"];
                    cnvp_deliverytemplate.RectangleName = dr["RectangleName"] is DBNull ? String.Empty : (string)dr["RectangleName"];
                    cnvp_deliverytemplate.ItemName = dr["ItemName"] is DBNull ? String.Empty : (string)dr["ItemName"];
                    cnvp_deliverytemplate.MarginTop = dr["MarginTop"] is DBNull ? 0 : (decimal)dr["MarginTop"];
                    cnvp_deliverytemplate.MarginLeft = dr["MarginLeft"] is DBNull ? 0 : (decimal)dr["MarginLeft"];
                    cnvp_deliverytemplate.RectangleWidth = dr["RectangleWidth"] is DBNull ? 0 : (decimal)dr["RectangleWidth"];
                    cnvp_deliverytemplate.RectangleHight = dr["RectangleHight"] is DBNull ? 0 : (decimal)dr["RectangleHight"];
                    list.Add(cnvp_deliverytemplate);
                }
            }
            return list;

        }

        #endregion
    }
}
